import store from '@/store';
import api from "@/api";
import { getMenuAllItem, getMenu, setMenuUri } from '@/utils/tree'

/*
 * @Desc: 获取菜单资源
 * @params: {String}
 * @return: {Object}
*/
export const getResources = ()=> {
    let resources = store.getters['menu/getResources'];
    return resources;
};

/*
 * @Desc: 获取菜单资源
 * @params: {String}
 * @return: {Object}
*/
export const setResources = async(cb) => {
  try{
    const { data } = await api.app.resourceGet();
    const menus = setMenuUri(data) // 是这基础 uri
    let menu = [...getMenuAllItem(menus)]
    // console.log(menu)
    await store.dispatch('menu/setResources',menu);
    await store.dispatch('menu/setMenu',menus);
    if (cb) {
      cb();
    }
    return menu;
  }catch(e){
    return [];
  }
}

export const removeResources = async () => {
  try {
    await store.dispatch('menu/setResources',[]);
  } catch (error) {
    console.info(error);
  }
}
